package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.repository.RegionRepsitory;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class RegionServiceImpl implements IRegionService {

    @Autowired
    private RegionRepsitory regionRepsitory;

    @Autowired
    private MongoTemplate template;


    //容易让mongodb产生误会,后续自动生成的id不会存进去
    @Override
    public void save(Region region) {
        region.setId(null);
        regionRepsitory.save(region);
    }

    @Override
    public void update(Region region) {
        regionRepsitory.save(region);
    }

    @Override
    public void delete(String id) {
        regionRepsitory.deleteById(id);
    }

    @Override
    public Region get(String id) {
        return regionRepsitory.findById(id).orElse(null);
    }

    @Override
    public List<Region> list() {
        return regionRepsitory.findAll();
    }


    //页面数据和分页查询
    @Override
    public Page<Region> query(RegionQuery qo) {

        //创建查询条件where
        Query query = new Query();
        //获取总条数
        //long total = template.count(query, Region.class);
        //返回空分页对象
       /* if (total == 0) {
            return Page.empty();
        }*/
        //当前页数据
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.ASC, "_id");

        //存入查询条件中
        //query.with(pageable);
        //查询内容
        //List<Region> list = template.find(query, Region.class);

        //返回PageImpl实现类,Page是接口不能返回
        //1:内容,2:当前页数据,3:总条数
        //return new PageImpl<>(list, pageable, total);
        //使用分页工具类
        return DBHelper.query(query, Region.class, template, pageable);

    }

    //保存或修改
    @Override
    public void saveOrUpdate(Region region) {
        if (!StringUtils.hasLength(region.getId())) {
            this.save(region);
        }
        this.update(region);
    }

    //设置或取消热门操作(ishot)
    @Override
    public void changeHotValue(String id, int hot) {
        //全量更新
        // Region region = this.get(id);
        //修改ishot状态
        // region.setIshot(hot);
        //修改
        //this.update(region);

        //部分更新(推荐使用)
        //db.集合名.updateMany({"id":id},{$set:{"ishot":hot}})
        //创建查询条件
        Query query = new Query();
        //放入查询条件
        query.addCriteria(Criteria.where("_id").is(id));
        //创建修改对象
        Update update = new Update();
        //设置ishot状态
        update.set("ishot", hot);
        //使用模板修改
        template.updateMulti(query, update, Region.class);
    }


    //查询热门目的地页面数据并排序
    @Override
    public List<Region> queryhotRegion() {
        //Region.STATE_HOT:热门为1
        List<Region> list = regionRepsitory.findByIshotOrderBySequence(Region.STATE_HOT);
        return list;
    }

}
